<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>花仙馆API测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #ff6b9d;
            text-align: center;
        }
        .test-section {
            margin: 20px 0;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        .test-section h3 {
            color: #333;
            margin-top: 0;
        }
        button {
            background: #ff6b9d;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            margin: 5px;
        }
        button:hover {
            background: #ff8fab;
        }
        .result {
            background: #f8f9fa;
            border: 1px solid #dee2e6;
            border-radius: 5px;
            padding: 10px;
            margin-top: 10px;
            white-space: pre-wrap;
            font-family: monospace;
            max-height: 300px;
            overflow-y: auto;
        }
        .success {
            border-color: #28a745;
            background-color: #d4edda;
        }
        .error {
            border-color: #dc3545;
            background-color: #f8d7da;
        }
        .status {
            display: inline-block;
            padding: 2px 8px;
            border-radius: 3px;
            font-size: 12px;
            font-weight: bold;
        }
        .status.success {
            background: #28a745;
            color: white;
        }
        .status.error {
            background: #dc3545;
            color: white;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🌸 花仙馆API测试页面</h1>
        
        <div class="test-section">
            <h3>1. 测试分类接口</h3>
            <button onclick="testCategories()">获取分类列表</button>
            <div id="categories-result" class="result"></div>
        </div>

        <div class="test-section">
            <h3>2. 测试商品接口</h3>
            <button onclick="testFlowers()">获取商品列表</button>
            <button onclick="testFeaturedFlowers()">获取推荐商品</button>
            <div id="flowers-result" class="result"></div>
        </div>

        <div class="test-section">
            <h3>3. 测试商品详情接口</h3>
            <button onclick="testFlowerDetail(1)">获取商品详情(ID:1)</button>
            <div id="detail-result" class="result"></div>
        </div>

        <div class="test-section">
            <h3>4. 服务器状态</h3>
            <div id="server-status">
                <span class="status" id="status-indicator">检测中...</span>
                <span id="status-text">正在检测服务器状态...</span>
            </div>
        </div>
    </div>

    <script>
        const API_BASE = 'http://127.0.0.1:8000/api/v1';
        
        // 通用请求函数
        async function apiRequest(url, options = {}) {
            try {
                const response = await fetch(API_BASE + url, {
                    method: 'GET',
                    headers: {
                        'Content-Type': 'application/json',
                        ...options.headers
                    },
                    ...options
                });
                
                const data = await response.json();
                return {
                    success: response.ok,
                    status: response.status,
                    data: data
                };
            } catch (error) {
                return {
                    success: false,
                    status: 0,
                    error: error.message
                };
            }
        }

        // 显示结果
        function showResult(elementId, result) {
            const element = document.getElementById(elementId);
            element.className = 'result ' + (result.success ? 'success' : 'error');
            
            if (result.success) {
                element.textContent = `✅ 请求成功 (${result.status})\n\n` + 
                    JSON.stringify(result.data, null, 2);
            } else {
                element.textContent = `❌ 请求失败 (${result.status})\n\n` + 
                    (result.error || JSON.stringify(result.data, null, 2));
            }
        }

        // 测试分类接口
        async function testCategories() {
            const result = await apiRequest('/flowers/categories/');
            showResult('categories-result', result);
        }

        // 测试商品接口
        async function testFlowers() {
            const result = await apiRequest('/flowers/');
            showResult('flowers-result', result);
        }

        // 测试推荐商品接口
        async function testFeaturedFlowers() {
            const result = await apiRequest('/flowers/featured/');
            showResult('flowers-result', result);
        }

        // 测试商品详情接口
        async function testFlowerDetail(id) {
            const result = await apiRequest(`/flowers/${id}/`);
            showResult('detail-result', result);
        }

        // 检测服务器状态
        async function checkServerStatus() {
            const statusIndicator = document.getElementById('status-indicator');
            const statusText = document.getElementById('status-text');
            
            try {
                const result = await apiRequest('/flowers/categories/');
                if (result.success) {
                    statusIndicator.className = 'status success';
                    statusIndicator.textContent = '在线';
                    statusText.textContent = '后端服务器运行正常';
                } else {
                    statusIndicator.className = 'status error';
                    statusIndicator.textContent = '异常';
                    statusText.textContent = `服务器响应异常 (${result.status})`;
                }
            } catch (error) {
                statusIndicator.className = 'status error';
                statusIndicator.textContent = '离线';
                statusText.textContent = '无法连接到后端服务器';
            }
        }

        // 页面加载时检测服务器状态
        window.onload = function() {
            checkServerStatus();
            
            // 每30秒检测一次服务器状态
            setInterval(checkServerStatus, 30000);
        };
    </script>
</body>
</html>